package linklist;

public class L142 {
    //困难题
    public ListNode detectCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while (fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if (slow==fast){
                ListNode curr1=head;
                ListNode curr2=fast;
                while (curr1!=curr2){
                    curr1=curr1.next;
                    curr2=curr2.next;
                }
                return curr1;
            }
        }
        return null;
    }

}
